package com.app.micaihu.ad.custom;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.ad.AdConfig;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.bean.ad.NativeAd;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.custom.components.downandupload.DownloadUtils;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.listener.AutoHeightControllerListener;
import com.app.micaihu.service.DownLoadService;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.view.main.game.GameDownloadActivity;
import com.app.okhttplib.OkHttpUtil;
import com.app.okhttplib.annotation.DownloadStatus;
import com.app.okhttplib.bean.DownloadFileInfo;
import com.app.utils.util.AppTools;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.baidu.mobstat.StatService;
import com.blankj.utilcode.util.AppUtils;

import java.io.File;
import java.util.HashMap;

/**
 * 游戏图片原生自定义View(适用于文章信息流)
 * Created by linkai on 2017/2/9.
 */
public class GameImgNativieView extends RelativeLayout implements View.OnClickListener {

    private NativeAd nativeAd;//显示的广告

    /************ 控件 *************/
    private CustomImageView smallImg;
    private CustomImageView bigImg;
    private CustomImageView oneImg;
    private CustomImageView twoImg;
    private CustomImageView threeImg;
    private TextView title;
    private TextView time;
    private TextView time1;
    private TextView time2;
    private ImageView mark;
    private ImageView mark1;
    private ImageView mark2;
    private TextView downloadView;//下载按钮

    private String typeName;//样式名称

    private int width = 324;
    private int height = 232;
    private AutoHeightControllerListener autoHeightControllerListener;

    private HashMap<String, Object> mapEvent;

    public GameImgNativieView(Context context) {
        super(context);
        initView(false);
    }

    public GameImgNativieView(Context context, HashMap<String, Object> mapEvent) {
        super(context);
        this.mapEvent = mapEvent;
        initView(false);
    }

    public GameImgNativieView(Context context, boolean isNoPadding, int width, int height) {
        super(context);
        this.width = width;
        this.height = height;
        initView(isNoPadding);
    }

    public GameImgNativieView(Context context, boolean isNoPadding, int width, int height, HashMap<String, Object> mapEvent) {
        super(context);
        this.width = width;
        this.height = height;
        this.mapEvent = mapEvent;
        initView(isNoPadding);
    }

    /**
     * 初始化公共布局
     */
    protected void initView(boolean isNoPadding) {
        if (isNoPadding) {
            LayoutInflater.from(getContext()).inflate(R.layout.ad_game_img_about, this);
        } else {
            LayoutInflater.from(getContext()).inflate(R.layout.ad_game_img, this);
        }
        this.title = (TextView) findViewById(R.id.item_public_title);
        this.time = (TextView) findViewById(R.id.ad_time);
        this.time1 = (TextView) findViewById(R.id.ad_time1);
        this.time2 = (TextView) findViewById(R.id.ad_time2);
        this.mark = (ImageView) findViewById(R.id.ad_mark);
        this.mark1 = (ImageView) findViewById(R.id.ad_mark1);
        this.mark2 = (ImageView) findViewById(R.id.ad_mark2);
        this.oneImg = (CustomImageView) findViewById(R.id.item_image_0);
        this.twoImg = (CustomImageView) findViewById(R.id.item_image_1);
        this.threeImg = (CustomImageView) findViewById(R.id.item_image_2);
        this.smallImg = (CustomImageView) findViewById(R.id.item_small_image);
        this.bigImg = (CustomImageView) findViewById(R.id.item_large_image);
        this.downloadView = (TextView) findViewById(R.id.ad_download);

        ViewSizeUtils.changeViewAuto1080(smallImg, width, height);
        ViewSizeUtils.changeViewAuto1080(oneImg, width, height);
        ViewSizeUtils.changeViewAuto1080(twoImg, width, height);
        ViewSizeUtils.changeViewAuto1080(threeImg, width, height);

        setOnClickListener(this);
        autoHeightControllerListener = new AutoHeightControllerListener(this.bigImg, 4.35f, 1.58f);
    }

    /**
     * 设置即将加载的广告
     *
     * @param nativeAd
     */
    public void setNativeAd(NativeAd nativeAd) {
//        MobclickAgentUtils.event(Config.EventBauduId.game_exposed, "推荐");
        StatService.onEvent(getContext(), "game_exposed", "推荐", 1);//统计事件的点击次数
        if (nativeAd == null) {
            return;
        }
        this.title.setText(nativeAd.getTitle());
        if (TextUtils.equals(nativeAd.getStyle(), AdConfig.AD_VIEW_TYPE_LIST_NATIVE_SMALL)) {

            if (nativeAd.getThumbList() != null && nativeAd.getThumbList().length > 0) {
                if (nativeAd.getThumbList().length < 3) {
                    LoadImageUtils.getInstance().loadImage(this.smallImg, nativeAd.getThumbList()[0]);
                    this.oneImg.setVisibility(View.GONE);
                    this.twoImg.setVisibility(View.GONE);
                    this.threeImg.setVisibility(View.GONE);
                    this.smallImg.setVisibility(View.VISIBLE);
                    this.bigImg.setVisibility(View.GONE);
                    //显示一张图的描述
                    this.time.setText(nativeAd.getPublishTime());
                    this.time.setVisibility(View.VISIBLE);
                    this.time1.setVisibility(View.GONE);
                    this.time2.setVisibility(View.GONE);
                    this.mark.setVisibility(View.VISIBLE);
                    this.mark1.setVisibility(View.GONE);
                    this.mark2.setVisibility(View.GONE);
                    if (this.downloadView.getVisibility() == View.VISIBLE) {
                        this.downloadView.setVisibility(View.GONE);
                    }
                    typeName = "一张图";
                } else {
                    LoadImageUtils.getInstance().loadImage(this.oneImg, nativeAd.getThumbList()[0]);
                    LoadImageUtils.getInstance().loadImage(this.twoImg, nativeAd.getThumbList()[1]);
                    LoadImageUtils.getInstance().loadImage(this.threeImg, nativeAd.getThumbList()[2]);
                    this.oneImg.setVisibility(View.VISIBLE);
                    this.twoImg.setVisibility(View.VISIBLE);
                    this.threeImg.setVisibility(View.VISIBLE);
                    this.smallImg.setVisibility(View.GONE);
                    this.bigImg.setVisibility(View.GONE);
                    //显示三张图的描述
                    this.time1.setText(nativeAd.getPublishTime());
                    this.time.setVisibility(View.GONE);
                    this.time1.setVisibility(View.VISIBLE);
                    this.time2.setVisibility(View.GONE);
                    this.mark.setVisibility(View.GONE);
                    this.mark1.setVisibility(View.VISIBLE);
                    this.mark2.setVisibility(View.GONE);
                    if (this.downloadView.getVisibility() == View.VISIBLE) {
                        this.downloadView.setVisibility(View.GONE);
                    }
                    typeName = "三张图";
                }
            } else {
                this.oneImg.setVisibility(View.GONE);
                this.twoImg.setVisibility(View.GONE);
                this.threeImg.setVisibility(View.GONE);
                this.smallImg.setVisibility(View.GONE);
                this.bigImg.setVisibility(View.GONE);
                this.time.setText(nativeAd.getPublishTime());
                this.time.setVisibility(View.VISIBLE);
                this.time1.setVisibility(View.GONE);
                this.time2.setVisibility(View.GONE);
                this.mark.setVisibility(View.VISIBLE);
                this.mark1.setVisibility(View.GONE);
                this.mark2.setVisibility(View.GONE);
                if (this.downloadView.getVisibility() == View.VISIBLE) {
                    this.downloadView.setVisibility(View.GONE);
                }
                typeName = "无图";
            }
        } else if (TextUtils.equals(nativeAd.getStyle(), AdConfig.AD_VIEW_TYPE_LIST_NATIVE_BIG)) {
            if (nativeAd.getThumbList() != null && nativeAd.getThumbList().length > 0) {
                LoadImageUtils.getInstance().loadImage(this.bigImg, nativeAd.getThumbList()[0], autoHeightControllerListener);
                this.oneImg.setVisibility(View.GONE);
                this.twoImg.setVisibility(View.GONE);
                this.threeImg.setVisibility(View.GONE);
                this.smallImg.setVisibility(View.GONE);
                this.bigImg.setVisibility(View.VISIBLE);
                this.time2.setText(nativeAd.getPublishTime());
                this.time2.setVisibility(View.VISIBLE);
                this.time1.setVisibility(View.GONE);
                this.time.setVisibility(View.GONE);
                this.mark2.setVisibility(View.VISIBLE);
                this.mark1.setVisibility(View.GONE);
                this.mark.setVisibility(View.GONE);
                if (this.downloadView.getVisibility() == View.GONE) {
                    this.downloadView.setVisibility(View.VISIBLE);
                }
                if (nativeAd.isinstall()) {
                    this.downloadView.setText("立即打开");
                } else {
                    this.downloadView.setText("立即下载");
                }
                typeName = "大图";
            } else {
                this.oneImg.setVisibility(View.GONE);
                this.twoImg.setVisibility(View.GONE);
                this.threeImg.setVisibility(View.GONE);
                this.smallImg.setVisibility(View.GONE);
                this.bigImg.setVisibility(View.GONE);
                this.time.setText(nativeAd.getPublishTime());
                this.time.setVisibility(View.VISIBLE);
                this.time1.setVisibility(View.GONE);
                this.time2.setVisibility(View.GONE);
                this.mark.setVisibility(View.VISIBLE);
                this.mark1.setVisibility(View.GONE);
                this.mark2.setVisibility(View.GONE);
                if (this.downloadView.getVisibility() == View.VISIBLE) {
                    this.downloadView.setVisibility(View.GONE);
                }
                typeName = "无图";
            }
        }

        this.nativeAd = nativeAd;
        this.setTag("suces");//设置标识表示加载广告成功
    }

    @Override
    public void onClick(View v) {
        if (nativeAd == null) {
            return;
        }
        if (mapEvent != null) {
            MobclickAgentUtils.event(UmEventId.game_click, mapEvent);
            MobclickAgentUtils.event(UmEventId.game_download, mapEvent);
        }
        StatService.onEvent(getContext(), "067", "推荐", 1);//统计事件的点击次数
        String type = "";
        boolean appInstalled = AppUtils.isAppInstalled(nativeAd.getPackageName());
        if (appInstalled) {
            type = AdConfig.TYPE_2;
            com.app.okhttplib.util.AppUtils.startUpApp(getContext(), nativeAd.getIPackageName());
        } else {
            File unInstallFile = new File(DownloadUtils.DOWNLOAD_FILEDIR, nativeAd.getIPath() + ".apk");
            if (unInstallFile.exists() && unInstallFile.isFile()) {//安装游戏
                com.app.okhttplib.util.AppUtils.installAPP(getContext(), unInstallFile);
            } else {
                DownloadFileInfo downloadFileInfo = DownloadUtils.getSingleton().getDownloadFileInfo(OkHttpUtil.getTag(nativeAd.getIUrl()));
                if (downloadFileInfo == null) {
                    GameDownloadActivity.insertGame(nativeAd.getIGameId(), nativeAd.getITitle(), nativeAd.getIUrl(), nativeAd.getIAppIcon(),
                            nativeAd.getSummary(), nativeAd.getPackageName(), nativeAd.getIPath(), nativeAd.getISize());
                    DownloadUtils.getSingleton().down(nativeAd.getIUrl(), nativeAd.getIPath(), nativeAd);
                } else {
                    String downloadStatus = downloadFileInfo.getDownloadStatus();
                    if (!DownloadStatus.DOWNLOADING.equals(downloadStatus)) {
                        GameDownloadActivity.insertGame(nativeAd.getIGameId(), nativeAd.getITitle(), nativeAd.getIUrl(), nativeAd.getIAppIcon(),
                                nativeAd.getSummary(), nativeAd.getPackageName(), nativeAd.getIPath(), nativeAd.getISize());
                        DownloadUtils.getSingleton().down(nativeAd.getIUrl(), nativeAd.getIPath(), nativeAd);
                    }
                }
            }

            StatService.onEvent(getContext(), "057", "文章列表" + typeName + "游戏下载", 1);// 统计的点击的次数
            type = AdConfig.TYPE_3;
        }


        String serialId = getTag().toString();
        if (!TextUtils.isEmpty(serialId)) {
            AdConfig.staticsAd(serialId, type, AdConfig.AD_TYPE_GAME);
        }
    }

}
